//
//  TrackProgressView.m
//  student
//
//  Created by JCai on 2018/10/25.
//  Copyright © 2018 higer. All rights reserved.
//

#import "TrackProgressView.h"

@interface TrackProgressView ()



@property (nonatomic, strong) UILabel *beginLabel;
@property (nonatomic, strong) UILabel *endLabel;
@property (nonatomic, strong) UIButton *rateButton;

@property (nonatomic) CGFloat progressWidth;
@property (nonatomic, strong) UIImageView *progressImageView;
@property (nonatomic, strong) UIImageView *slideImageView;
@property (nonatomic, strong) UIImageView *carImageView;

@end


@implementation TrackProgressView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setBackgroundColor:[UIColor colorWithWhite:0 alpha:0.4f]];
        
        CGRect infoFrame = CGRectMake(10 * SCALING,
                                      10 * SCALING,
                                      40 * SCALING,
                                      20 * SCALING);
        UILabel *beginLabel = [[UILabel alloc] initWithFrame:infoFrame];
        [beginLabel setTextColor:[UIColor whiteColor]];
        [beginLabel setBackgroundColor:[UIColor clearColor]];
        [beginLabel setTextAlignment:NSTextAlignmentCenter];
        [beginLabel setFont:[UIFont systemFontOfSize:10.0f* SCALING]];
        [self addSubview:beginLabel];
        self.beginLabel = beginLabel;
        
        CGRect otherFrame = CGRectMake(CGRectGetWidth(frame) - 90 * SCALING,
                                       CGRectGetMinY(infoFrame),
                                       80 *SCALING,
                                       20 * SCALING);
        
        CGRect progressFrame = CGRectMake(CGRectGetMaxX(infoFrame),
                                          CGRectGetMinY(infoFrame),
                                          CGRectGetMinX(otherFrame) - CGRectGetMaxX(infoFrame),
                                          20 * SCALING);
        [self initProgress:progressFrame];
        
        
        [self otherFrame:otherFrame];
    }
    return self;
}

#pragma mark -
- (void)initProgress:(CGRect)frame
{
    _progressWidth = CGRectGetWidth(frame);
    CGRect progressFrame = CGRectMake(CGRectGetMinX(frame),
                                      CGRectGetMinY(frame) + (CGRectGetHeight(frame)-6.0f)/2.0f,
                                      _progressWidth,
                                      6.0f);
    UIImageView *progressImageView = [[UIImageView alloc] initWithFrame:progressFrame];
    [progressImageView setBackgroundColor:[UIColor whiteColor]];
    progressImageView.layer.masksToBounds = YES;
    progressImageView.layer.cornerRadius = 3.0f;
    progressImageView.layer.borderWidth = 1.0f;
    progressImageView.layer.borderColor = HEXCOLOR(0x21d4a4).CGColor;
    [self addSubview:progressImageView];
    self.progressImageView = progressImageView;

    CGRect slideFrame = progressFrame;
    slideFrame.size.width = 0;
    UIImageView *slideImageView = [[UIImageView alloc] initWithFrame:slideFrame];
    slideImageView.layer.masksToBounds = YES;
    slideImageView.layer.cornerRadius = 3.0f;
    [slideImageView setBackgroundColor:HEXCOLOR(0x21d4a4)];
    [self addSubview:slideImageView];
    self.slideImageView = slideImageView;
    
    
    CGRect carFrame = CGRectMake(CGRectGetMinX(progressFrame)-4,
                                 CGRectGetMidY(progressFrame) - 12,
                                 20,
                                 20);
    UIImageView *carImageView = [[UIImageView alloc] initWithFrame:carFrame];
    [carImageView setImage:[UIImage imageNamed:@"TeachSimulationPlayCar"]];
    [self addSubview:carImageView];
    self.carImageView = carImageView;
}

#pragma mark -
- (void)reloadProgress:(NSInteger)begin
{
    if (self.endTime != 0) {
        int s = (int)begin % 60;
        int m = (int)begin / 60;
        
        [self.beginLabel setText:[NSString stringWithFormat:@"%02d:%02d", m, s]];
        
        CGFloat per = begin / (CGFloat)(self.endTime);
        CGFloat slideWidth = _progressWidth * per;
        if (slideWidth >= _progressWidth) {
            slideWidth = _progressWidth;
        }
        
        CGRect slideFrame = self.slideImageView.frame;
        slideFrame.size.width = slideWidth;
        
        CGRect catFrame = self.carImageView.frame;
        if (slideWidth >= catFrame.size.width - 6) {
            catFrame.origin.x = CGRectGetMaxX(slideFrame) - catFrame.size.width+2;
        }
        WS(ws);
        [UIView animateWithDuration:0.4 animations:^{
            [ws.slideImageView setFrame:slideFrame];
            [ws.carImageView setFrame:catFrame];
        }];
    }
}

- (void)setEndTime:(NSInteger)endTime
{
    _endTime = endTime;
    int s = (int)endTime % 60;
    int m = (int)endTime / 60;

    [self.endLabel setText:[NSString stringWithFormat:@"%02d:%02d", m, s]];
}

#pragma mark -
- (void)otherFrame:(CGRect)frame
{
    CGRect tempFrame = CGRectMake(CGRectGetMinX(frame),
                                  CGRectGetMinY(frame),
                                  CGRectGetWidth(frame) / 2.0f,
                                  CGRectGetHeight(frame));
    UILabel *endLabel = [[UILabel alloc] initWithFrame:tempFrame];
    [endLabel setTextColor:[UIColor whiteColor]];
    [endLabel setBackgroundColor:[UIColor clearColor]];
    [endLabel setTextAlignment:NSTextAlignmentCenter];
    [endLabel setFont:[UIFont systemFontOfSize:10.0f* SCALING]];
    [self addSubview:endLabel];
    self.endLabel = endLabel;
    
    CGRect rateFrame = CGRectMake(CGRectGetMaxX(tempFrame), 0, CGRectGetWidth(tempFrame), 40 * SCALING);
    UIButton *rateButton = [[UIButton alloc] initWithFrame:rateFrame];
    [rateButton setBackgroundColor:[UIColor clearColor]];
    [rateButton.titleLabel setFont:[UIFont boldSystemFontOfSize:14.0f * SCALING]];
    [rateButton addTarget:self action:@selector(rateClicked:) forControlEvents:UIControlEventTouchUpInside];
    [rateButton setTag:1];
    [rateButton setTitle:@"1.0X" forState:UIControlStateNormal];
    [self addSubview:rateButton];
    self.rateButton = rateButton;
}

- (void)rateClicked:(UIButton *)button
{
    NSInteger tag = button.tag;
    tag += 1;
    if (tag > 3) {
        tag = 1;
    }
    [button setTag:tag];
    [button setTitle:[NSString stringWithFormat:@"%@.0X", @(tag)] forState:UIControlStateNormal];
    
    if (self.rateBlock) {
        self.rateBlock(tag);
    }
}


@end
